﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace Exhibition.Common.Meta
{
    public class CommFunction
    {

        public static void SetMeta(string key, string des, System.Web.UI.Page page)
        {
            HtmlMeta keywords = new HtmlMeta(),// keywords
            description = new HtmlMeta(),
            title=new HtmlMeta(); // description
            keywords.Name = "keywords";
            keywords.Content = key; 
            description.Name = "description";
            description.Content = des;
             
            page.Controls.Add(keywords);
            page.Controls.Add(description);
            //Page..Controls.Add(keywords);
            //Page.Header.Controls.Add(description);
        }

        /// <summary>
        /// 装载表头
        /// </summary>
        /// <param name="tablename"></param>
        /// <returns></returns>
        public static List<ReportTableColumn> LoadReportTableColumn(string tablename)
        {

            var tables = GetConfigTables(tablename);

            var cols = from col in tables.Elements()
                       select new ReportTableColumn(col, "Name", "SourceName");
            return cols.ToList();
        }


        private static IEnumerable<XElement> GetConfigTables(string tbname)
        {
            try
            {
                XElement config = XElement.Load(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["TablePath"]));
                var tables = from ele in config.Descendants("Table")
                             where ele.Attribute("Name").Value == tbname
                             select ele;
                return tables;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public class ReportTable
        {

            public ReportTable(XElement tb, string tbname)
            {
                this.tableName = tb.Attribute(tbname).Value;
            }

            public string tableName { get; set; }
            public List<ReportTableColumn> columns { get; set; }

        }

        public class ReportTableColumn
        {

            public ReportTableColumn(XElement columnElement, string column, string sourcecolumn)
            {
                this.columnName = columnElement.Attribute(column).Value;
                this.sourceColumnName = columnElement.Attribute(sourcecolumn).Value;
                this.dtype = columnElement.Attribute("DataType").Value;
            }

            public string columnName { get; set; }
            public string sourceColumnName { get; set; }
            public string dtype { get; set; }
        }


    }
}
